package com.ph.irp.sell.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.dto.SellPriceDTO;
import com.ph.irp.dto.SellPriceDetailDTO;
import com.ph.irp.model.PSellSellprice;
import com.ph.irp.model.PSellSellpricedetail;
import com.ph.irp.sell.service.SellPriceService;
import com.ph.util.DateUtil;
import com.ph.util.StringUtil;
import com.ph.util.pagination.PageUtil;

public class SellPriceAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PSellSellprice sellPrice = null;
	
	private SellPriceService sellPriceService = null;
	
	private List<SellPriceDTO> sellPriceDTOList = null;
	private List<SellPriceDTO> tempSellPriceDTOList = null;
	private List<SellPriceDetailDTO> sellPriceDetailDTOList = null;
	
	private String successString;
	
	private Object[] rowlist = null;
	private static final String SP = "SP";
	private HttpServletRequest request;
	
	//分页信息
	private String cpage;
	private PageDTO page;
	
	private String [] materialId;
	private String [] productCode;
	private String [] productName;
	private String [] standardType;
	private String [] baseUnitName;
	private Float [] baseUnitNoStart;
	private Float [] baseUnitNoEnd;
	private String [] unit;
	private Float [] amountStart;
	private Float [] amountEnd;
	private Float [] unitPrice;
	private Float [] addTaxUnitPrice;
	private Float [] taxRate;
	private Float [] discountRate;
	private Float [] realAddTaxUnitPrice;
	private String [] remark;
	
	private String FORWARD = null;
	/***
	 * 新增
	 * @return
	 */
	public String addSellPrice() throws Throwable{
		
		
		List<PSellSellpricedetail> sellPriceDetailList = new ArrayList<PSellSellpricedetail>();
		
		if(materialId.length!=0){
			for(int i=0;i<materialId.length;i++){
				PSellSellpricedetail sellPriceDetail = new PSellSellpricedetail();
				
				sellPriceDetail.setMaterialId(Integer.valueOf(materialId[i]));
				if(addTaxUnitPrice[i]==null){
					addTaxUnitPrice[i]=0.00f;
				}
				sellPriceDetail.setAddTaxUnitPrice(Float.valueOf(addTaxUnitPrice[i]));
				sellPriceDetail.setAmountEnd(Float.valueOf(amountEnd[i]));
				sellPriceDetail.setAmountStart(Float.valueOf(amountStart[i]));
				sellPriceDetail.setBaseUnitNoEnd(Float.valueOf(baseUnitNoEnd[i]));
				sellPriceDetail.setBaseUnitName(String.valueOf(baseUnitName[i]));
				sellPriceDetail.setBaseUnitNoStart(Float.valueOf(baseUnitNoStart[i]));
				if(discountRate[i]==null){
					discountRate[i]=0.00f;
				}
				sellPriceDetail.setDiscountRate(Float.valueOf(discountRate[i]));
				sellPriceDetail.setProductCode(String.valueOf(productCode[i]));
				sellPriceDetail.setProductName(String.valueOf(productName[i]));
				if(realAddTaxUnitPrice[i]==null){
					realAddTaxUnitPrice[i]=0.00f;
				}
				sellPriceDetail.setRealAddTaxUnitPrice(Float.valueOf(realAddTaxUnitPrice[i]));
				sellPriceDetail.setRemark(String.valueOf(remark[i]));
				sellPriceDetail.setStandardType(String.valueOf(standardType[i]));
				sellPriceDetail.setTaxRate(Float.valueOf(taxRate[i]));
				sellPriceDetail.setUnit(String.valueOf(unit[i]));
				if(unitPrice[i]==null){
					unitPrice[i]=0.00f;
				}
				sellPriceDetail.setUnitPrice(Float.valueOf(unitPrice[i]));
				
				sellPriceDetailList.add(sellPriceDetail) ;
			}
		}
		
		sellPrice.setCheckState(0);
		sellPrice.setBlankOutState(0);

		
		if(sellPrice.getCheckDate().trim().equals("")){
			sellPrice.setCheckDate(null);
		}
	
		sellPriceService.addSellPrice(sellPrice, sellPriceDetailList) ;
		
		sellPrice = null;
		FORWARD = "success";
		return FORWARD;
	}
	/***
	 * 显示所有
	 * @return
	 */
	public String showSellPriceList() throws Throwable {
		sellPrice = null;
		tempSellPriceDTOList = this.sellPriceService.getAllSellPrice();
		page = PageUtil.generatePageDTO(tempSellPriceDTOList,
				"showSellPriceList.action", cpage);
		sellPriceDTOList = page.getResList() ;
		FORWARD = "success";
		return FORWARD;

	}
	
	/***
	 * 显示新增
	 * @return
	 */
	public String showAddSellPrice() throws Throwable {
		request = ServletActionContext.getRequest();
		
		String selldate = DateUtil.getCurrentDateString();
		String makebiller = (String) request.getSession().getAttribute("userName");
		//自动生成编码
		String sellpricecode = null;
		sellpricecode= SP + StringUtil.generateSequenceNumber(sellPriceService.getNewestSellPriceCode(), SP);

		sellPrice = new PSellSellprice();
			
		sellPrice.setSellPriceCode(sellpricecode);
		sellPrice.setSellDate(selldate);
		sellPrice.setMakeBiller(makebiller);
			
		FORWARD = "success";
		return FORWARD;
	}
	/***
	 * 删除
	 * @return
	 */	
	public String deleteSellPrice()throws Throwable{
		
		this.sellPriceService.deleteSellPrice(rowlist);
		
		
		FORWARD = "success";
		return FORWARD;
	}

	
	/***
	 * 查询ByCondition
	 * @return
	 */	
	public String getSellPriceByCondition() throws Throwable{
		String code = sellPrice.getSellPriceCode().trim();
		String dateFrom = sellPrice.getSellDate().trim();
		String dateTo = sellPrice.getCheckDate().trim();
		int state = sellPrice.getCheckState();
		int customerId = sellPrice.getCustomerId();
		
		sellPrice.setSellPriceCode(code);
		sellPrice.setSellDate(dateFrom);
		sellPrice.setCheckDate(dateTo);
		sellPrice.setCheckState(state);
		sellPrice.setCustomerId(customerId);
		
		sellPriceDTOList = this.sellPriceService.getSellPriceByCondition(sellPrice);
		
		sellPrice = null;
		FORWARD = "success";
		return FORWARD;
		
	}
	
	/***
	 * 查询明细
	 * @return
	 */	
	public String showSellPriceDetail() throws Throwable{
		
		sellPriceDTOList = this.sellPriceService.getSellPriceDTOById(Integer.valueOf((String) rowlist[0]));
		
		sellPriceDetailDTOList = this.sellPriceService.getSellPriceDetailBySellPriceId(Integer.valueOf((String) rowlist[0]));
		
		FORWARD = "success";
		return FORWARD;
	}
	
	public PSellSellprice getSellPrice() {
		return sellPrice;
	}
	public void setSellPrice(PSellSellprice sellPrice) {
		this.sellPrice = sellPrice;
	}
	public SellPriceService getSellPriceService() {
		return sellPriceService;
	}
	public void setSellPriceService(SellPriceService sellPriceService) {
		this.sellPriceService = sellPriceService;
	}
	public List<SellPriceDTO> getSellPriceDTOList() {
		return sellPriceDTOList;
	}
	public void setSellPriceDTOList(List<SellPriceDTO> sellPriceDTOList) {
		this.sellPriceDTOList = sellPriceDTOList;
	}
	public Object[] getRowlist() {
		return rowlist;
	}
	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public String[] getMaterialId() {
		return materialId;
	}
	public void setMaterialId(String[] materialId) {
		this.materialId = materialId;
	}
	public String[] getProductCode() {
		return productCode;
	}
	public void setProductCode(String[] productCode) {
		this.productCode = productCode;
	}
	public String[] getProductName() {
		return productName;
	}
	public void setProductName(String[] productName) {
		this.productName = productName;
	}
	public String[] getStandardType() {
		return standardType;
	}
	public void setStandardType(String[] standardType) {
		this.standardType = standardType;
	}
	public String[] getBaseUnitName() {
		return baseUnitName;
	}
	public void setBaseUnitName(String[] baseUnitName) {
		this.baseUnitName = baseUnitName;
	}
	public Float[] getBaseUnitNoStart() {
		return baseUnitNoStart;
	}
	public void setBaseUnitNoStart(Float[] baseUnitNoStart) {
		this.baseUnitNoStart = baseUnitNoStart;
	}
	public Float[] getBaseUnitNoEnd() {
		return baseUnitNoEnd;
	}
	public void setBaseUnitNoEnd(Float[] baseUnitNoEnd) {
		this.baseUnitNoEnd = baseUnitNoEnd;
	}

	public String[] getUnit() {
		return unit;
	}
	public void setUnit(String[] unit) {
		this.unit = unit;
	}
	public Float[] getAmountStart() {
		return amountStart;
	}
	public void setAmountStart(Float[] amountStart) {
		this.amountStart = amountStart;
	}
	public Float[] getAmountEnd() {
		return amountEnd;
	}
	public void setAmountEnd(Float[] amountEnd) {
		this.amountEnd = amountEnd;
	}
	public Float[] getUnitPrice() {
		return unitPrice;
	}
	public void setUnitPrice(Float[] unitPrice) {
		this.unitPrice = unitPrice;
	}
	public Float[] getAddTaxUnitPrice() {
		return addTaxUnitPrice;
	}
	public void setAddTaxUnitPrice(Float[] addTaxUnitPrice) {
		this.addTaxUnitPrice = addTaxUnitPrice;
	}
	public Float[] getTaxRate() {
		return taxRate;
	}
	public void setTaxRate(Float[] taxRate) {
		this.taxRate = taxRate;
	}
	public Float[] getDiscountRate() {
		return discountRate;
	}
	public void setDiscountRate(Float[] discountRate) {
		this.discountRate = discountRate;
	}
	public Float[] getRealAddTaxUnitPrice() {
		return realAddTaxUnitPrice;
	}
	public void setRealAddTaxUnitPrice(Float[] realAddTaxUnitPrice) {
		this.realAddTaxUnitPrice = realAddTaxUnitPrice;
	}
	public String[] getRemark() {
		return remark;
	}
	public void setRemark(String[] remark) {
		this.remark = remark;
	}
	public String getFORWARD() {
		return FORWARD;
	}
	public void setFORWARD(String forward) {
		FORWARD = forward;
	}
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public static String getSP() {
		return SP;
	}
	public List<SellPriceDTO> getTempSellPriceDTOList() {
		return tempSellPriceDTOList;
	}
	public void setTempSellPriceDTOList(List<SellPriceDTO> tempSellPriceDTOList) {
		this.tempSellPriceDTOList = tempSellPriceDTOList;
	}
	public String getCpage() {
		return cpage;
	}
	public void setCpage(String cpage) {
		this.cpage = cpage;
	}
	public PageDTO getPage() {
		return page;
	}
	public void setPage(PageDTO page) {
		this.page = page;
	}
	public String getSuccessString() {
		return successString;
	}
	public void setSuccessString(String successString) {
		this.successString = successString;
	}
	public List<SellPriceDetailDTO> getSellPriceDetailDTOList() {
		return sellPriceDetailDTOList;
	}
	public void setSellPriceDetailDTOList(
			List<SellPriceDetailDTO> sellPriceDetailDTOList) {
		this.sellPriceDetailDTOList = sellPriceDetailDTOList;
	}

	
	

	

	
}
